package fx.mine.active.component.stock;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONPath;
import fx.mine.active.component.util.HttpUtil;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import lombok.extern.log4j.Log4j2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Log4j2
public class StockPane extends AnchorPane {
    private static final ThreadPoolExecutor EXECUTOR =
            new ThreadPoolExecutor(
                    1, 1, 0,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(10));
    SimpleStringProperty num = new SimpleStringProperty("12000.01");

    public StockPane(double width, double height) {
        setPrefSize(width, height);

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefSize(width, height);
        anchorPane.getStyleClass().add("web");
        AnchorPane.setBottomAnchor(anchorPane,20.0);
        AnchorPane.setTopAnchor(anchorPane,20.0);
        AnchorPane.setLeftAnchor(anchorPane,50.0);
        AnchorPane.setRightAnchor(anchorPane,50.0);

        GridPane gridPane = new GridPane();
        gridPane.getStyleClass().add("stockContainer");

        var vbox = new VBox();
        AnchorPane.setLeftAnchor(gridPane, 50.0);
        AnchorPane.setTopAnchor(gridPane, 20.0);
        AnchorPane.setRightAnchor(gridPane, 50.0);
        AnchorPane.setBottomAnchor(gridPane, 20.0);

        vbox.setSpacing(1.0);
        Text stockName = new Text("阿里巴巴");
        vbox.getChildren().add(stockName);

        Text stockNum = new Text();
        stockNum.textProperty().bind(num);

        stockNum.setFill(Color.RED);
        vbox.getChildren().add(stockNum);
        vbox.getStyleClass().add("stockDesc");
        vbox.setOnMouseClicked(event -> {
            WebView webView = new WebView();
            AnchorPane.setBottomAnchor(webView, 0.0);
            AnchorPane.setRightAnchor(webView, 0.0);
            AnchorPane.setLeftAnchor(webView, 0.0);
            AnchorPane.setTopAnchor(webView, 0.0);
            WebEngine engine = webView.getEngine();
            engine.setJavaScriptEnabled(true);
            engine.load("http://quote.eastmoney.com/us/BABA.html#fullScreenChart");
            ObservableList<Node> children = anchorPane.getChildren();
            //快讯示例
//            {"type":"data","cname":"bdc02c361aab973818f3583fb8b5e6d5","seq":61035,"content":"{\"Type\":\"MajorNews\",\"Data\":{\"Art_Code\":\"202209082505288872\",\"Art_Title\":\"23年来首次 欧央行加息75个基点！通胀“捅破天” 德国民众开始洗冷水澡\",\"Art_Sim_Title\":\"重大资讯推送\",\"Art_Docu_Reader\":\"23年来首次，欧央行加息75个基点！通胀“捅破天”，能源价格高涨，德国有些民众已开始洗冷水澡。\",\"Art_ShowTime\":\"2022-09-08T22:06:00\",\"Art_Url\":\"http://finance.eastmoney.com/a/202209082505288872.html\"}}"}

            if (children.isEmpty()) {
                children.add(webView);
            }
            anchorPane.toFront();
        });

        gridPane.addRow(0, vbox);
        getChildren().add(anchorPane);
        getChildren().add(gridPane);

        EXECUTOR.execute(() -> {
            UpdateTask task = new UpdateTask(num);
            HttpUtil.sseCallBack(s -> {
                try {
                    Integer nowPoint = (Integer) JSONPath.eval(s, "$.data.f43");
                    if (nowPoint == null) {
                        return;
                    }
                    double div = NumberUtil.div(nowPoint.doubleValue(), 100, 2);
                    task.callBack(div + "");
                } catch (Exception e) {
                    log.error(e);
                }
            });
        });
        getStyleClass().add("stockPane");
        getStylesheets().add(getClass().getResource("/css/stockPane.css").toExternalForm());
    }

    public static class UpdateTask extends Task<Void> {
        protected SimpleStringProperty simpleStringProperty;
        protected String now;

        public UpdateTask(SimpleStringProperty simpleStringProperty) {
            this.simpleStringProperty = simpleStringProperty;
        }

        public void callBack(String now) {
            try {
                this.now = now;
                call();
            } catch (Exception e) {
                log.error(e);
            }
        }

        @Override
        protected Void call() throws Exception {
            simpleStringProperty.set(now);
            return null;
        }
    }
}
